define('components/window', ['text!/components/window.html'], function(template){
  const data = {
    userAccount: '',
    userPassword: '',
    userPassword2: '',
    userName: ''
  };
  return {
    template: template,
    data: function () {
      return data
    },
    ready: function(){
      var vm = this;
      window.alert = function(s){
        vm.box.name = 'alert';
        vm.box.data = s;
      };
      window.input = function(s, cb, data){
        data = data || {};
        vm.box.name = 'input';
        vm.box.data = s;
        vm.box.placeholder = data.placeholder || '';
        vm.box.type = data.type || 'text';
        vm.box.callback = cb;
      };
    },
    computed: {
      success_account: function () {
        if (!/^[a-z0-9]+([._\\-]*[a-z0-9])*@([a-z0-9]+[-a-z0-9]*[a-z0-9]+.){1,63}[a-z0-9]+$/.test(this.userAccount)) {
          return false;
        }
        return true;
      },
      success_password: function () {
        if (this.userPassword.length < 6 || this.userPassword.length > 20) {
          return false;
        }
        return true;
      },
      success_password2: function () {
        if (this.userPassword2.length < 6 || this.userPassword2.length > 20) {
          return false;
        } else if (this.userPassword2 !== this.userPassword) {
          return false;
        }
        return true;
      },
      success_name: function () {
        if (this.userName.length < 2 || this.userName.length > 6) {
          return false;
        }
        return true;
      }
    },
    methods: {
      inputSubmit: function(){
        this.box.callback(this.box.val);
      },
      close: function () {
        this.box.name = false;
      },
      login: function () {
        var vm = this;
        if (vm.success_account !== true || vm.success_password !== true) {
          return;
        }
        require(['lib/md5'], function(){
          vm.$http.post('/user/login', {
            account: data.userAccount,
            password: hex_md5(data.userPassword)
          }).then(function (res) {
            res = res.data;
            if (res.code !== 0) {
              alert(res.message);
              return;
            }

            var userData = vm.user;
            var ds = res.data;
            for(var i in ds){
              userData[i] = ds[i];
            }
            userData.isLogin = true;

            vm.box.name = false;
          });
        });
      },
      register: function () {
        var vm = this;
        if (vm.success_account !== true || vm.success_password !== true || vm.success_password2 !== true || vm.success_name !== true) {
          return;
        }
        require(['lib/md5'], function(){
          vm.$http.post('/user/register', {
            account: data.userAccount,
            userName: data.userName,
            password: hex_md5(data.userPassword)
          }).then(function (res) {
            res = res.data;
            if (res.code !== 0) {
              alert(res.message);
              return;
            }
            var userData = vm.user;
            var ds = res.data;
            for(var i in ds){
              userData[i] = ds[i];
            }
            userData.isLogin = true;

            vm.box.name = false;
          });
        });
      }
    },
    props: ['box', 'user'],
    replace: true
  };
});

